package cn.lyvust.first.api;


import cn.lyvust.first.common.annotation.SysLogs;
import cn.lyvust.first.common.bean.ResponseCode;
import cn.lyvust.first.common.bean.ResponseResult;
import cn.lyvust.first.common.exception.RequestException;
import cn.lyvust.first.common.util.BeansUtil;
import cn.lyvust.first.manage.dto.param.evaluation.ProjectItemParam;
import cn.lyvust.first.manage.entity.EvaluationProjectItem;
import cn.lyvust.first.manage.service.EvaluationProjectItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * @author ggzheng
 * @since 2021-01-21
 */
@Api(tags = {"指标项Item"})
@RestController
@RequestMapping("/evaluation/project_item")
public class EvaluationProjectItemController {
    private static final Logger LOGGER = LoggerFactory.getLogger(EvaluationProjectItemController.class);
    @Autowired
    private EvaluationProjectItemService evaluationProjectItemService;

    @PostMapping(value = {"/list"})
    @ApiOperation(value = "查询指标项目")
    public ResponseResult list(@RequestBody ProjectItemParam param){
        return ResponseResult.e(ResponseCode.OK, evaluationProjectItemService.listAll(param));
    }

    @PostMapping(value = {"/add"})
    @ApiOperation(value = "新增一个指标项")
    @SysLogs("新增一个指标项")
    public ResponseResult add(@RequestBody ProjectItemParam param){
        if (StringUtils.isEmpty(param.getTitle())) {
            throw RequestException.fail("添加失败,指标项名称不能为空!");
        }
        if (StringUtils.isEmpty(param.getCreateBy())) {
            throw RequestException.fail("添加失败,指标项添加操作人姓名不能为空!");
        }
        EvaluationProjectItem entity = new EvaluationProjectItem();
        BeanUtils.copyProperties(param, entity);
        return ResponseResult.e(ResponseCode.OK, evaluationProjectItemService.saveOrUpdate(entity));
    }

    @PostMapping(value = {"/update"})
    @ApiOperation(value = "修改一个指标项")
    @SysLogs("修改一个指标项")
    public ResponseResult update(@RequestBody ProjectItemParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,指标项主键ID不能为空!");
        }
        if (StringUtils.isEmpty(param.getUpdateBy())) {
            throw RequestException.fail("修改失败,修改操作人姓名不能为空!");
        }
        EvaluationProjectItem entity = evaluationProjectItemService.getById(param.getId());
        BeansUtil.copyPropertiesIgnoreNull(param, entity);
        return ResponseResult.e(ResponseCode.OK, evaluationProjectItemService.updateById(entity));
    }

    @PostMapping(value = {"/delete"})
    @ApiOperation(value = "删除一个指标项")
    @SysLogs("删除一个指标项")
    public ResponseResult delete(@RequestBody ProjectItemParam param){
        if (StringUtils.isEmpty(param.getId())) {
            throw RequestException.fail("修改失败,指标项主键ID不能为空!");
        }
        return ResponseResult.e(ResponseCode.OK, evaluationProjectItemService.removeById(param.getId()));
    }
}
